<?php

require_once('../models/category.php');
require_once('../models/authorization.php');

/**
 * This class manipulates the category information retrieved from
 * the category table so that it can be used in the views.
 * 
 * @author Matt Makins
 */
class CategoryController{
		
	function addCategory($name){
		
		//if($session->checkLogin())
		$this->category->addCategory($name);	
   		header("Location: manageCategories.php");
	}
	
	function editCategory($id, $name){
		
		$this->category->editCategory($id, $name);
		header("Location: manageCategories.php");
	}
	
	function deleteCategory($id){

		// TODO: check user is logged in first
		$this->category->deleteCategory($id);		
   		header("Location: manageCategories.php");
	}
		
	function getAllCategories(){
		
		$categories = array();
		foreach(Category::findAllCategories() as $c){
			array_push($categories, array("title" => $c->getName(),
										  "id" => $c->getId()));			
		}		
		return $categories;
	}
	
	function getAllCategoryIds(){
		
		$cat_ids = array();
		foreach(Category::findAllCategories() as $c){
			array_push($cat_ids, $c->getId());
		}
		return $cat_ids;
	}
	
	function getAllCategoryNames(){

		$cat_names = array();
		foreach(Category::findAllCategories() as $c){
			array_push($cat_names, ltrim(rtrim($c->getName())));
		}
		return $cat_names;
	}
	
	function getCategoryNameById($id){
		
		foreach(Category::findAllCategories() as $c){
			if($c->getId() == $id)
				return $c->getName();
		}
		return null;
	}
		
	function getNumberOfCategories(){
		return $this->numberOfCategories;
	}	
}
?>
